using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

using MKB.Exceptions;

namespace MKB.TimePicker
{
    /// <summary>
    /// Validator used to ensure selected times are not in conflict with existing scheduled items.
    /// </summary>
    public class TimeSelectorAvailabilityValidator : BaseValidator
    {
        private TimeSelector _TimeSelector;

        #region Properties
        /// <summary>
        /// A list of DateTime values that represent already scheduled appointments
        /// </summary>
        public List<DateTime> AppointmentList
        {
            get 
            { 
                if(ViewState["AppointmentList"] == null)
                    return null;
                else
                    return (List<DateTime>)ViewState["AppointmentList"]; 
            }
            set { ViewState["AppointmentList"] = value; }
        }

        private int _AppointmentLengthInMinutes = 30;
        /// <summary>
        /// The amount of time in minutes the average appointment will last. Enables logic that will prevent overlapping of appointments.
        /// </summary>
        [DefaultValue(30)]
        public int AppointmentLengthInMinutes
        {
            get { return _AppointmentLengthInMinutes; }
            set { _AppointmentLengthInMinutes = value; }
        }

        protected override bool ControlPropertiesValid()
        {
            Control ctrl = FindControl(ControlToValidate);

            if (ctrl != null)
            {
                _TimeSelector = (TimeSelector)ctrl;
                return (_TimeSelector != null);
            }
            else
                return false;  // raise exception
        }

        /// <summary>
        /// String value indicating the id of a <see cref="MKB.TimePicker.TimeSelector"/> control.
        /// </summary>
        [
        Category("Behavior"),
        DefaultValue(""),
        Description("The control to validate."),
        TypeConverter(typeof(TimeSelectorValidationControlConverter)),
        IDReferenceProperty(typeof(TimeSelector))
        ]
        public string ControlToValidate
        {
            get
            {
                object o = ViewState["ControlToValidate"];
                return ((o == null) ? String.Empty : (string)o);
            }
            set
            {
                ViewState["ControlToValidate"] = value;
            }
        }



        protected override bool EvaluateIsValid()
        {
            foreach(DateTime dt in AppointmentList)
                if ((_TimeSelector.Date > dt.AddMinutes(-1 * AppointmentLengthInMinutes)) && 
                    (_TimeSelector.Date < dt.AddMinutes(AppointmentLengthInMinutes)))
                    return false;

            return true;
        }
        #endregion

        public TimeSelectorAvailabilityValidator()
        {
            base.EnableClientScript = false;
            if(ErrorMessage == string.Empty)
                ErrorMessage = "Your proposed time overlapped with another appointment. Please choose another time.";
        }
        protected override void OnPreRender(EventArgs e)
        {
            if (AppointmentList == null)
                throw new NullAppointmentListException("A value must be assigned to the appointment list for validation to occur.");
            base.OnPreRender(e);
        }
    }
}